package com.tpp.baseConsumer.service.impl;

import com.tpp.baseConsumer.feign.ItemFeign;
import com.tpp.baseConsumer.mapper.ImageMapper;
import com.tpp.baseConsumer.service.ImageService;
import com.tpp.baseConsumer.vo.BannerVo;
import com.tpp.comm.pojo.TpImage;
import com.tpp.comm.pojo.TpItem;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ImageServiceImpl implements ImageService {

    @Resource
    private ImageMapper imageMapper;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private ItemFeign itemFeign;

    @Override
    public List<BannerVo> queryBanner() {
        LambdaQueryWrapper<TpImage> wrapper= Wrappers.lambdaQuery();
        wrapper.eq(TpImage::getType,1).orderByAsc(TpImage::getSort);
        List<TpImage> list=imageMapper.selectList(wrapper);
        List<BannerVo> bannerVos = list.stream().map(tpImage->{
            BannerVo vo = new BannerVo();
            vo.setId(tpImage.getId());
            vo.setImgUrl(tpImage.getImgUrl());
            vo.setTargetId(tpImage.getTargetId());
            TpItem tpItem=restTemplate.getForObject("http://item/itemClient/findById?itemId="+tpImage.getTargetId(),TpItem.class);
            vo.setItemName(tpItem.getItemName());
            vo.setMinPrice(tpItem.getMinPrice());
            return vo;
        }).collect(Collectors.toList());
        return bannerVos;
    }
}
